/**
 * Name        : FileDialogNew.cpp
 * Version     : 1.0.0
 * Vendor      : badadev.com
 * Description : Component for creating new files/directories
 * Lincense    : This program is free software. It comes without any warranty, to
 *               the extent permitted by applicable law. You can redistribute it
 *               and/or modify it under the terms of the Do What The Fuck You Want
 *               To Public License, Version 2, as published by Sam Hocevar. See
 *               http://sam.zoy.org/wtfpl/COPYING for more details.
 */

#include "FileDialogNew.h"
#include "macros.h"

using namespace badadev;
using namespace Osp::Base;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;
using namespace Osp::Graphics;
using namespace Osp::Io;


FileDialogNew::FileDialogNew() {
}

FileDialogNew::~FileDialogNew() {
	this->RemoveAllControls();
}


void
FileDialogNew::Initialize(const Osp::Base::String & title, InheritanceContainer &parent,
						  Osp::Base::String &path, Osp::Base::String ext, const Osp::Graphics::Dimension d)
{


	this->Construct(true, Dimension(d.width, d.height/3*2) );
	this->setParent(parent);
	this->SetTitleText(title);
	this->path_ = path;
	this->supportExt_ = ext;

	// calculate from d
	int space=d.height/30;
	int c_w = d.width-10;	//
	int c_h = d.height/10; // component heigh
	int line = 0;          // component number

	/*
	cbtFile_ = new CheckButton();
	cbtFile_->Construct(Rectangle( 5, line*c_h+line*space, c_w, c_h ),CHECK_BUTTON_STYLE_RADIO,BACKGROUND_STYLE_NONE, false, L"New File");
	cbtFile_->SetActionId(FileDialogNew::ON_CREATE_FILE_TRUE, FileDialogNew::ON_CREATE_FILE_FALSE, 0);
	cbtFile_->AddActionEventListener(*this);
	this->AddControl(*cbtFile_);
	line++;

	cbtFolder_ = new CheckButton();
	cbtFolder_->Construct(Rectangle( 5, line*c_h+line*space, c_w, c_h ),CHECK_BUTTON_STYLE_RADIO,BACKGROUND_STYLE_NONE, false, L"New Dir");
	cbtFolder_->SetActionId(FileDialogNew::ON_CREATE_FOLDER_TRUE, FileDialogNew::ON_CREATE_FOLDER_FALSE,0);
	cbtFolder_->AddActionEventListener(*this);
	this->AddControl(*cbtFolder_);

	cbtGroup_ = new RadioGroup();
	cbtGroup_->Construct();
	cbtGroup_->Add(*cbtFile_);
	cbtGroup_->Add(*cbtFolder_);
	cbtFile_->SetSelected(true);
	line++;
*/
	lbName_ = new Label();
	lbName_->Construct(Rectangle( 5, line*c_h+line*space, c_w, c_h ), S( "IDS_WITH_NAME" ) );
	this->AddControl(*lbName_);
	line++;

	efName_ = new EditField();
	efName_->Construct(Rectangle( 5, line*c_h+line*space, c_w-10, c_h*2));
	this->AddControl(*efName_);
	line++;
	line++;

	btCreate_ = new Button();
	btCreate_->Construct(Rectangle( 5, line*c_h+line*space, c_w/3, c_h), S( "IDS_CREATE" ) );
	btCreate_->SetActionId(FileDialogNew::ON_CREATE);
	btCreate_->AddActionEventListener(*this);
	this->AddControl(*btCreate_);
	btCancel_ = new Button();
	btCancel_->Construct(Rectangle( c_w-c_w/3, line*c_h+line*space, c_w/3, c_h), S( "IDS_CANCEL" ) );
	btCancel_->SetActionId(FileDialogNew::ON_CANCEL);
	btCancel_->AddActionEventListener(*this);
	this->AddControl(*btCancel_);

}


void
FileDialogNew::OnActionPerformed(const Osp::Ui::Control& source, int actionId){
	switch(actionId)
	{
	    case(FileDialogNew::ON_CANCEL):
	    	this->SetShowState( false );
			this->sendContainerActionToParent(InheritanceContainer::ON_CLOSE_ACTION);
		break;
	    case(FileDialogNew::ON_CREATE): {


	    	newfilepath_ = "";
			newfolderpath_= "";

	    	this->SetShowState( false );

			AppLog ( "On Create FDN" );

			newfilepath_.Clear();
			if ( path_.EndsWith(L"/") ) {
				newfilepath_ = String(path_ + efName_->GetText());
			} else	{
				newfilepath_ = String(path_ + String("/") + efName_->GetText());
			}

			// add support extension
    		if ( File::GetFileExtension( newfilepath_ ) != supportExt_ ) {
    			newfilepath_.Append( "." );
    			newfilepath_.Append( supportExt_ );
    		}

/*// nop create file/dir
	    	if(cbtFile_->IsSelected())    	{

	    		// file
	    		File newfile;
	    		if ( File::GetFileExtension( t_path ) != supportExt_ ) {
	    			t_path.Append( "." );
	    			t_path.Append( supportExt_ );
	    		}

	    		newfilepath_ = t_path;
	    		newfile.Construct( t_path ,"w+"));
	    	}  	else   	{
	    		// dir
	    		newfolderpath_ = t_path;
	    		AppLog ( "create doodle %S", File::GetFileName( t_path ).GetPointer() );
	    		if(IsFailed(Directory::Create( t_path )))
	    			AppLog("Failed to create %S", t_path.GetPointer());
	    	}
*/
    		this->sendContainerActionToParent(FileDialogNew::ON_CREATE);
	    }
		break;


		default:
				AppLog ( "default action on FDNew" );
		break;
	}


}

